﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using MassOperations.vs2008.Model;
using System.Text;

namespace MassOperations.vs2008.Reader
{
	public class CSVStorageReader:IStorageReader
	{
		private readonly string _filename;
		private readonly string _separator;

		public CSVStorageReader(string filename, string separator)
		{
			this._filename = filename;
			this._separator = separator;
		}

		#region IStorageReader Members

		public IEnumerable<StorageItem> ReadAll()
		{
			return File
				.ReadAllLines(_filename, Encoding.GetEncoding(1251))
				.Select(line => line.Split(new[] {_separator}, StringSplitOptions.None))
				.Select(pair => new StorageItem(pair[0], pair[1]));
		}

		#endregion
		
		public override string ToString()
		{
			return new FileInfo(_filename).Name;
		}
	}
}
